package com.whp.yky.yky.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.entity.DocumentEntity;
import com.whp.yky.yky.entity.Process;
import com.whp.yky.yky.entity.poentity.Document;
import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.impl.DocumentEntityServiceImpl;
import com.whp.yky.yky.service.impl.ProcessServiceImpl;
import com.whp.yky.yky.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whp
 * @since 2021-03-15
 */
@RestController
@CrossOrigin
public class DocumentEntityController {

    @Autowired
    private DocumentEntityServiceImpl documentEntityService;

    @Autowired
    private ProcessServiceImpl processService;

    @PostMapping("/getDocumentEntityInfo")
    public ResponseData getDocumentEntityInfo(@RequestBody DocumentEntity documentEntity, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<DocumentEntity> pageList = this.documentEntityService.getDocumentEntityInfo(new Page( (long)pageNum,(long)pageSize),documentEntity);
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @GetMapping("/getDocumentEntityDetail")
    public ResponseData getDocumentEntityDetail( @RequestParam Integer id){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        DocumentEntity documentEntity = documentEntityService.getById(id);
        res.setData(documentEntity);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @DeleteMapping("/deletDocumentEntityByID/{id}")
    @ApiOperation(value="删除档案", notes="根据id删除档案")
    public ResponseData deletDocumentEntityByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = documentEntityService.removeById(id);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }



    @ApiOperation(value="修改档案信息", notes="修改档案信息")
    @PostMapping("/updateDocumentEntityInfo")
    public ResponseData updateDocumentInfo(@RequestBody DocumentEntity documentEntity,@RequestParam String token){


        boolean result = false;
        ResponseData res = new ResponseData();
        Process process =new Process();
        DecodedJWT verify = JwtUtil.verify(token);
        process.setOperatorId(Integer.valueOf(verify.getClaim("id").asString()));
        process.setStatus(0);


        if(documentEntity.getId()==0){
            result= documentEntityService.save(documentEntity);
        }else{
            result= documentEntityService.updateById(documentEntity);
        }

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }
}

